/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author BAGUS
 */
public class Pemesanan {

    private String kodePemesanan;
    private String waktuPemesanan;
    private double jumlahBarang;
    private String idBarang;
    private String username;
    private String status;
    private double jumlahBarangTerkirim;
    private double jumlahBarangBelumTerkirim;
    private String kodeUpdate;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    public String getKodePemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodePemesanan from Pemesanan order by kodePemesanan");
            String kode_Pemesanan = "";
            Date now = new Date();
            SimpleDateFormat dateFormatterTanggal = new SimpleDateFormat("d");
            SimpleDateFormat dateFormatterBulan = new SimpleDateFormat("M");
            SimpleDateFormat dateFormatterTahun = new SimpleDateFormat("y");
            String tanggal = dateFormatterTanggal.format(now);
            String bulan = dateFormatterBulan.format(now);
            String tahun = dateFormatterTahun.format(now);
            String substring = tahun.substring(2);
            String kodeTanggal = tanggal + bulan + substring;
            while (resultSet.next()) {
                kode_Pemesanan = resultSet.getString("kodePemesanan");
            }
            int no_Pemesanan = 0;
            if ("".equals(kode_Pemesanan)) {
                this.kodePemesanan = kodeTanggal + "201";
            } else {
                String data = null;
                if (kode_Pemesanan.length() == 9) {
                    data = kode_Pemesanan.substring(0, 6);
                } else {
                    data = kode_Pemesanan.substring(0, 5);
                }
                if (kodeTanggal.equals(data)) {
                    no_Pemesanan = Integer.parseInt(kode_Pemesanan) + 1;
                    this.kodePemesanan = String.valueOf(no_Pemesanan);
                } else {
                    this.kodePemesanan = kodeTanggal + "201";
                }
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            this.kodePemesanan = null;
        }
        return kodePemesanan;
    }

    public String getWaktuPemesanan() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPemesanan = simpleDateFormat.format(date);
        return waktuPemesanan;
    }

    public double getJumlahBarang() {
        return jumlahBarang;
    }

    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    public String getIdBarang() {
        return idBarang;
    }

    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void setKodeUpdate(String kodeUpdate) {
        this.kodeUpdate = kodeUpdate;
    }

    public String getKodeUpdate() {
        return kodeUpdate;
    }

    public void setJumlahBarangTerkirim(double jumlahBarangTerkirim) {
        this.jumlahBarangTerkirim = jumlahBarangTerkirim;
    }

    public double getJumlahBarangTerkirim() {
        return jumlahBarangTerkirim;
    }

    public void setJumlahBarangBelumTerkirim(double jumlahBarangBelumTerkirim) {
        this.jumlahBarangBelumTerkirim = jumlahBarangBelumTerkirim;
    }

    public double getJumlahBarangBelumTerkirim() {
        return jumlahBarangBelumTerkirim;
    }

    public void insertPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            double jumlahbarang2 = 0;
            if ("belum".equals(getStatus())) {
                statement.executeQuery("insert into pemesanan values ('"
                        + getKodePemesanan() + "','"
                        + getWaktuPemesanan() + "',"
                        + getJumlahBarang() + ",'"
                        + getIdBarang() + "','"
                        + getUsername() + "','"
                        + getStatus() + "',0,"
                        + getJumlahBarang() + ")");
            } else if ("tanggungan".equals(getStatus())) {
                ResultSet resultSet1 = statement.executeQuery("select jumlahbarang from pemesanan where kodepemesanan = '" + getKodeUpdate() + "'");
                while (resultSet1.next()) {
                    jumlahbarang2 = resultSet1.getDouble("jumlahbarang");
                }
                statement.executeUpdate("update pemesanan "
                        + "set status = 'tanggungan', "
                        + "jumlahbarangterkirim = " + getJumlahBarang() + ", "
                        + "jumlahbarangbelumterkirim = " + (jumlahbarang2 - getJumlahBarang()) + ", "
                        + "jumlahbarang = " + (jumlahbarang2 - getJumlahBarang()) + " where kodepemesanan = '" + getKodeUpdate() + "'");
            } else {

                statement.executeUpdate("update pemesanan "
                        + "set status = 'terkirim', "
                        + "jumlahbarangterkirim = " + getJumlahBarang() + ", "
                        + "jumlahbarangbelumterkirim = 0 "
                        + "where kodepemesanan = '" + getKodeUpdate() + "'");
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public ResultSet tampilPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from pemesanan");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public double tampilJumlahBarangPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahbarang from pemesanan where kodepemesanan = '" + getKodeUpdate() + "'");

            double jumlahbarang1 = 0;
            while (resultSet.next()) {
                jumlahbarang1 = resultSet.getDouble("jumlahbarang");
            }
            return jumlahbarang1;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return 0;
        }
    }

    public String tampilIdBarang(String kodePemesanan) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select idbarang from pemesanan where kodepemesanan = '" + kodePemesanan + "'");
            while (resultSet.next()) {
                return resultSet.getString("idbarang");
            }
            return "0";
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return "0";
        }
    }

    public boolean bandingJumlahBarangPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahbarang from pemesanan where kodepemesanan = '" + getKodeUpdate() + "'");
            String jumlah1 = "0";
            while (resultSet.next()) {
                jumlah1 = resultSet.getString("jumlahbarang");
            }
            double jumlah2 = Double.parseDouble(jumlah1);
            if (getJumlahBarang() <= jumlah2) {
                return true;
            }
            return false;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return false;
        }
    }

    public ResultSet tampilKodePemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepemesanan from pemesanan where status in ('belum','tanggungan')");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }
}
